﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using C1.Phone.Chart;
using Microsoft.Phone.Shell;

namespace ControlExplorer.Controls.Chart
{
    public partial class FinancialChart
        : UserControl, IExposeApplicationBarItems
    {

        ApplicationBarIconButton btnNewData = new ApplicationBarIconButton(new Uri("/Resources/AppBar/plus.png" , UriKind.Relative));
        public FinancialChart()
        {
            InitializeComponent();

            chart.Actions.Add(new ScaleAction()); // double-tap
            chart.Actions.Add(new TranslateAction()); // drag
            chart.View.AxisX.Scale = 0.20;
            
            btnNewData.Text = "New Data";
            btnNewData.Click += btnNewData_Click;

            Loaded += FinancialChart_Loaded;
        }

        void FinancialChart_Loaded(object sender, RoutedEventArgs e)
        {
            NewData();
        }

        void btnNewData_Click(object sender, EventArgs e)
        {
            NewData();
        }

        void NewData()
        {
            long ticks = DateTime.Now.Ticks;
            int len = 40;
            List<Quotation> data = SampleFinancialData.Create(len);
            chart.BeginUpdate();
            chart.Data.ItemsSource = data;
            //chart.View.AxisX.Min = data[0].Time.ToOADate() - 0.5;
            //chart.View.AxisX.Max = data[len - 1].Time.ToOADate() + 0.5;
            chart.EndUpdate();
        }


        #region IExposeApplicationBarItems Members

        public IEnumerable<IApplicationBarMenuItem> ApplicationBarItems
        {
            get 
            {
                yield return btnNewData;
            }
        }

        #endregion
    }

    public class Quotation
    {
        public DateTime Time
        {
            get;
            set;
        }

        public double Open
        {
            get;
            set;
        }

        public double Close
        {
            get;
            set;
        }

        public double High
        {
            get;
            set;
        }

        public double Low
        {
            get;
            set;
        }

        public double Volume
        {
            get;
            set;
        }
    }

    public class SampleFinancialData
    {
        static Random rnd = new Random();
        public static List<Quotation> Create(int npts)
        {
            List<Quotation> data = new List<Quotation>();
            DateTime dt = DateTime.Today.AddDays(0);

            for (int i = 0; i < npts; i++)
            {
                Quotation q = new Quotation();

                q.Time = dt.AddDays(i);

                if (i > 0)
                    q.Open = data[i - 1].Close;
                else
                    q.Open = 1000;

                q.High = q.Open + rnd.Next(50);
                q.Low = q.Open - rnd.Next(50);

                q.Close = rnd.Next((int)q.Low, (int)q.High);

                q.Volume = rnd.Next(0, 100);

                data.Add(q);
            }

            return data;
        }
    }
}